from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
from app01.robot.robot_qa import QuestionClassifier, QuestionPaser, AnswerSearcher


classifier = QuestionClassifier()

parser = QuestionPaser()

searcher = AnswerSearcher()


def index(request):
    return render(request, 'index.html', {"n1": "王雄俊"})


def ajax(request):
    # 获取对方发送的消息
    question = request.GET.get('text')
    # print(question)
    res_classify = classifier.classify(question)  # 从问题中提取出实体和关系
    if len(res_classify) <= 1:
        return JsonResponse({'message': []}, safe=False)
    print(res_classify)
    res_cypher = parser.parser_main(res_classify)  # 生成Cyper语句
    final_answers = searcher.search_main(res_cypher)  # 生成回答
    print(final_answers)
    return JsonResponse({'message': final_answers}, safe=False)

